package com.cn.springcloudshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.springcloudshop.common.AjaxRes;
import com.cn.springcloudshop.common.Const;
import com.cn.springcloudshop.model.YmShopReceivingAddress;
import com.cn.springcloudshop.model.YmShopReceivingAddressExample;
import com.cn.springcloudshop.model.YmUser;
import com.cn.springcloudshop.service.inter.ReceivingAddressService;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: wuyanzu
 * @Date: 2018/8/21
 */
@RequestMapping("/address/")
@RestController
public class ReceivingAddressController {

    @Autowired
    ReceivingAddressService receivingAddressService;

    @RequestMapping("add")
    public AjaxRes addAddress(AjaxRes ajaxRes, String ymUser, String address) {
        YmUser user = ShopCarController.getYmUser(ymUser);
        System.err.println(user.getUserId()+"==============");
        if (user != null) {
            if (StringUtils.isNotEmpty(address)) {
                YmShopReceivingAddress ymShopReceivingAddress = JSONObject.parseObject(address, YmShopReceivingAddress.class);
                ymShopReceivingAddress.setUserId(user.getUserId());
                if (receivingAddressService.insert(ymShopReceivingAddress) == 1) {
                    ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.SAVE_FAIL);
                }
            } else {
                ajaxRes.setFailMsg(Const.PARAMETER_NULL);
            }
        } else {
            ajaxRes.setFailMsg("用户信息有误");
        }
        return ajaxRes;
    }

    @RequestMapping("default")
    public AjaxRes getDefaultAddress(AjaxRes ajaxRes, String ymUser, @RequestParam(required = false)Long receivingId) {
        YmUser user = ShopCarController.getYmUser(ymUser);
        System.err.println(user.getUserId()+"==============");
        if (user != null) {
            if (receivingId == 0) {
                YmShopReceivingAddressExample example = new YmShopReceivingAddressExample();
                example.createCriteria().andUserIdEqualTo(user.getUserId()).andSateEqualTo("Y").andIsDefaultEqualTo("Y");
                List<YmShopReceivingAddress> address = receivingAddressService.selectByExample(example);
                if (address != null && address.size() == 1) {
                    ajaxRes.setSucceed(address.get(0), Const.DATA_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.DATA_FAIL);
                }
            } else {
                YmShopReceivingAddress address = receivingAddressService.selectByPrimaryKey(receivingId);
                if (address != null) {
                    ajaxRes.setSucceed(address, Const.DATA_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.DATA_FAIL);
                }
            }
        } else {
            ajaxRes.setFailMsg("用户信息有误");
        }
        return ajaxRes;
    }
    @RequestMapping("getAddress")
    public AjaxRes getAddress(AjaxRes ajaxRes,@RequestParam(required = false) String ymUser, @RequestParam(required = false) Long receivingId) {
        YmUser user = ShopCarController.getYmUser(ymUser);
        System.err.println(user.getUserId()+"==============");
        if (user != null) {
            if (receivingId == null) {
                YmShopReceivingAddressExample example = new YmShopReceivingAddressExample();
                example.createCriteria().andUserIdEqualTo(user.getUserId()).andSateEqualTo("Y");
                List<YmShopReceivingAddress> list = receivingAddressService.selectByExample(example);
                if (list != null && list.size() > 0) {
                    ajaxRes.setSucceed(list, Const.DATA_SUCCEED);
                } else {
                    ajaxRes.setSucceed(null, "请添加地址信息");
                }
            } else {
                YmShopReceivingAddress address = receivingAddressService.selectByPrimaryKey(receivingId);
                if (address != null) {
                    ajaxRes.setSucceed(address, Const.DATA_SUCCEED);
                } else {
                    ajaxRes.setFailMsg(Const.DATA_FAIL);
                }
            }
        }else {
            ajaxRes.setFailMsg("用户信息有误");
        }

        return ajaxRes;
    }

    @RequestMapping("del")
    public AjaxRes delAddressByPk(AjaxRes ajaxRes,Long receivingId) {
        if (receivingId != null) {
            if (receivingAddressService.deleteByPrimaryKey(receivingId) == 1) {
                ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
            } else {
                ajaxRes.setFailMsg(Const.DEL_FAIL);
            }
        } else {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        }
        return ajaxRes;
    }

    @RequestMapping("update")
    public AjaxRes updateByPk(AjaxRes ajaxRes,Long receivingId,@RequestParam(required = false) String receiverName,
                              @RequestParam(required = false) String receiverPhone,@RequestParam(required = false)Long receiverState,
                              @RequestParam(required = false)Long receiverCity,@RequestParam(required = false)Long receiverDistrict,
                              @RequestParam(required = false)String receiverAddress,@RequestParam(required = false)String detailedAddress,
                              @RequestParam(required = false)String isDefault) {
        if (receivingId != null) {
            YmShopReceivingAddress ymShopReceivingAddress = receivingAddressService.selectByPrimaryKey(receivingId);
            if (!"".equals(receiverName) && receiverName != null) {
                ymShopReceivingAddress.setReceiverName(receiverName);
            }
            if (!"".equals(receiverPhone) && receiverPhone != null) {
                ymShopReceivingAddress.setReceiverPhone(receiverPhone);
            }
            if (receiverState != null) {
                ymShopReceivingAddress.setReceiverState(receiverState);
            }
            if (receiverCity != null) {
                ymShopReceivingAddress.setReceiverCity(receiverCity);
            }
            if (receiverDistrict != null) {
                ymShopReceivingAddress.setReceiverDistrict(receiverDistrict);
            }
            if (!"".equals(receiverAddress) && receiverAddress != null) {
                ymShopReceivingAddress.setReceiverAddress(receiverAddress);
            }
            if (!"".equals(detailedAddress) && detailedAddress != null) {
                ymShopReceivingAddress.setDetailedAddress(detailedAddress);
            }
            if (!"".equals(isDefault) && isDefault != null) {
                ymShopReceivingAddress.setIsDefault(isDefault);
            }
            if (receivingAddressService.updateByPrimaryKey(ymShopReceivingAddress) == 1) {
                ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
            } else {
                ajaxRes.setFailMsg(Const.UPDATE_FAIL);
            }
        } else {
            ajaxRes.setFailMsg(Const.PARAMETER_NULL);
        }
        return ajaxRes;
    }
}
